<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use think\exception\ValidateException;
use think\facade\Log;
use think\facade\Validate;
use think\cache\driver\Redis;

/**
 * Order控制器
 */
class Ordersearch extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

    }


    /**
     * 列表
     */
    public function index()
    {
        $data = [
            'code' => 1,
            'msg' => '',
            'count' => 0,
            'data' => [],
        ];

        return json($data);

        $limits = $this->request->get('limit', 10);
        $page = $this->request->get('page', 1);
        $newpage = $page - 1;
        if ($newpage == 0) {
            $limit1 = 0;
            $limit2 = $limits;
        } else {
            $limit1 = $newpage * $limits;
            $limit2 = $limits;
        }
        $filters = $this->request->get('filter', '{}');
        $filters = json_decode($filters, true);
        $where2 = array();
        if (!empty($filters['name'])) {
            $where2[] = array('a.name', 'LIKE', '%' . trim($filters['name']) . '%');
        }
        if (!empty($filters['site_id'])) {
            $where2[]  = ['a.site_id', '=', $filters['site_id']];
        }
        if (!empty($filters['shipping_name'])) {
            $where2[] = array('a.shipping_name', 'LIKE', '%' . $filters['shipping_name'] . '%');
        }
        if (!empty($filters['tracking_number'])) {
            $where2[] = array('a.tracking_number', 'LIKE', '%' . $filters['tracking_number'] . '%');
        }
        if (!empty($filters['email'])) {
            $where2[] = array('a.email', 'LIKE', '%' . $filters['email'] . '%');
        }
        if (!empty($filters['sku'])) {
            $where2[] = array('c.sku', 'LIKE', '%' . $filters['sku'] . '%');
        }
        if (!empty($filters['financial_status'])) {
            $filters['financial_status'] = $this->financial_status_switch($filters['financial_status']);
            $where2[]  = ['a.financial_status', '=', $filters['financial_status']];
        }

        if (!empty($filters['created_at'])) {
            $where2[]  = ['a.created_at', '>=', strtotime($filters['created_at'][0])];
            $where2[]  = ['a.created_at', '<=', strtotime($filters['created_at'][1])];
        }
        // halt($where2);
        if (empty($where2) && $page == 1 && $limits == 10) {
            $redis = new Redis();
            $lists = $redis->get('lists_order');
            if (empty($lists)) {
                $where3[] = ['c.sku', '>', 0];
                $lists = table('order')
                    ->alias('a')
                    ->field('a.id,a.name,a.email,a.shipping_name,b.variant_title,a.tracking_number,a.total_price,a.fulfillment_status,a.created_at,a.site_id,a.financial_status,c.sku,b.quantity,a.updatetime,a.is_mark')
                    ->join('qu_order_quantity b', 'a.id = b.order_id', 'left')
                    ->join('qu_order_sku c', 'b.variant_id = c.variants_id', 'left')
                    ->where($where2)
                    ->where($where3)
                    ->group('a.id')
                    ->order('a.created_at desc')
                    ->limit($limit1, $limit2)
                    ->select()->toarray();
                foreach ($lists as $k => $v) {
                    $site = table('site')
                        ->where(['id' => $v['site_id']])->find();
                    if (!empty($site)) {
                        $lists[$k]['site_name'] = $site['site_name'];
                    } else {
                        $lists[$k]['site_name'] = '未知';
                    }
                    $lists[$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
                    $lists[$k]['updatetime'] = date('Y-m-d H:i:s', $v['updatetime']);
                    $lists[$k]['financial_status'] = $this->financial_status_switch($v['financial_status']);
                    $lists[$k]['fulfillment_status'] = $this->fulfillment_status_switch($v['fulfillment_status']);
                }
                $redis->set('lists_order', $lists, 60 * 5);
            }
        } else {
            $where3[] = ['c.sku', '>', 0];
            $lists = table('order')
                ->alias('a')
                ->field('a.id,a.name,a.email,a.shipping_name,b.variant_title,a.tracking_number,a.total_price,a.fulfillment_status,a.created_at,a.site_id,a.financial_status,c.sku,b.quantity,a.updatetime,a.is_mark')
                ->join('qu_order_quantity b', 'a.id = b.order_id', 'left')
                ->join('qu_order_sku c', 'b.variant_id = c.variants_id', 'left')
                ->where($where2)
                ->where($where3)
                ->group('a.id')
                ->order('a.created_at desc')
                ->limit($limit1, $limit2)
                ->select()->toarray();
            foreach ($lists as $k => $v) {
                $site = table('site')
                    ->where(['id' => $v['site_id']])->find();
                if (!empty($site)) {
                    $lists[$k]['site_name'] = $site['site_name'];
                } else {
                    $lists[$k]['site_name'] = '未知';
                }
                $lists[$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
                $lists[$k]['updatetime'] = date('Y-m-d H:i:s', $v['updatetime']);
                $lists[$k]['financial_status'] = $this->financial_status_switch($v['financial_status']);
                $lists[$k]['fulfillment_status'] = $this->fulfillment_status_switch($v['fulfillment_status']);
            }
        }

        $where3[] = ['c.sku', '>', 0];
        $count = table('order')
            ->alias('a')
            ->join('qu_order_quantity b', 'a.id = b.order_id', 'left')
            ->join('qu_order_sku c', 'b.variant_id = c.variants_id', 'left')
            ->where($where2)
            ->where($where3)
            ->group('a.id')
            ->count();

        $data = [
            'code' => 1,
            'msg' => '',
            'count' => (int)$count,
            'data' => $lists,
        ];

        return json($data);
    }
}
